/***************************************************************************
 创建者：          hualei
 开始时间：      2018.09.10
 copyright:(C)    深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************/
#include "../../programView/programview.h"
#include "modbusstartinstructionview.h"
#include "ui_modbusstartinstructionview.h"

ModbusStartInstructionView::ModbusStartInstructionView(int styleId,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ModbusStartInstructionView)
{
    ui->setupUi(this);
    initialDisplayStyle(styleId);
}

void ModbusStartInstructionView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->forward_Btn->setGeometry(10,350,70,40);
        ui->save_Btn->setGeometry(360,350,70,40);
        break;
    default:
        break;
    }
}

void ModbusStartInstructionView::getInput()
{
    noteStr = ui->note_lineEdit->text();
    modbusID = ui->modbusID_Edit->text().toInt();

    int modbusType_TCP_RS485 = ui->INTERNET_RS485_ComBox->currentIndex();
    int modbusType_MASTER_SLAVE = ui->MASTER_SLAVE_ComboBox->currentIndex();
    if((modbusType_TCP_RS485 == 0) && (modbusType_MASTER_SLAVE == 0))
    {
        modbusSpecType = E_MODBUS_SPEC_TYPE_INTERNET_MASTER;
    }
    else if((modbusType_TCP_RS485 == 0) && (modbusType_MASTER_SLAVE == 1))
    {
        modbusSpecType = E_MODBUS_SPEC_TYPE_INTERNET_SLAVE;
    }
    else if((modbusType_TCP_RS485 == 1) && (modbusType_MASTER_SLAVE == 0))
    {
        modbusSpecType = E_MODBUS_SPEC_TYPE_RS485_MASTER;
    }
    else if((modbusType_TCP_RS485 == 1) && (modbusType_MASTER_SLAVE == 1))
    {
        modbusSpecType = E_MODBUS_SPEC_TYPE_RS485_SLAVE;
    }
    else
    {
        modbusSpecType = E_MODBUS_SPEC_TYPE_ERROR;
    }

    JMP_LB_Velue = ui->JMP_LB_Edit->text().toInt();

}

void ModbusStartInstructionView::updateView()
{
    ui->note_lineEdit->setText(noteStr);
    ui->modbusID_Edit->setText(QString::number(modbusID));
    ui->JMP_LB_Edit->setText(QString::number(JMP_LB_Velue));

    if(modbusSpecType == E_MODBUS_SPEC_TYPE_INTERNET_MASTER)
    {
        ui->INTERNET_RS485_ComBox->setCurrentIndex(0);
        ui->MASTER_SLAVE_ComboBox->setCurrentIndex(0);
    }
    else if(modbusSpecType == E_MODBUS_SPEC_TYPE_INTERNET_SLAVE)
    {
        ui->INTERNET_RS485_ComBox->setCurrentIndex(0);
        ui->MASTER_SLAVE_ComboBox->setCurrentIndex(1);
    }
    else if(modbusSpecType == E_MODBUS_SPEC_TYPE_RS485_MASTER)
    {
        ui->INTERNET_RS485_ComBox->setCurrentIndex(1);
        ui->MASTER_SLAVE_ComboBox->setCurrentIndex(0);
    }
    else if(modbusSpecType == E_MODBUS_SPEC_TYPE_RS485_SLAVE)
    {
        ui->INTERNET_RS485_ComBox->setCurrentIndex(1);
        ui->MASTER_SLAVE_ComboBox->setCurrentIndex(1);
    }
//    else if(modbusSpecType == E_MODBUS_SPEC_TYPE_ERROR)
//    {
//        ui->INTERNET_RS485_ComBox->setEditable(false);
//        ui->MASTER_SLAVE_ComboBox->setEditable(false);
//    }

}

void ModbusStartInstructionView::changeLanguage(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/modbusStartViewEnglishLan.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
    ui->retranslateUi(this);
}

void ModbusStartInstructionView::on_clearNote_Btn_clicked()
{
    ui->note_lineEdit->clear();
    emit cleanNote();
}

void ModbusStartInstructionView::on_forward_Btn_clicked()
{
    this->close();
    programView->instructionListFlag = 0;
    programView->editInstructionViewShowOrHide();
}

void ModbusStartInstructionView::on_save_Btn_clicked()
{
    getInput();
    emit finishEidtInstruction();
    emit saveInstruction(instruction);
}

